const ruleFuc={
  alert(text){
    wx.showToast({
      icon:'none',
      title: text,
    })
  },
  error(text,vm){
    console.log(text,vm)
    vm.setData({
      show:false,
      text
    })
  },
  noNone(e,title,params,vm){
    if(e.detail.value === ''){
      title = title.replace(':','')
      this.error(`${title}输入不能为空`,vm)
      return false
    }
    return true
  },
  maxLength(e,title,maxLength,vm){
    if(e.detail.value.length > +maxLength){
      title = title.replace(':','')
      this.error(`${title}最大可输入${maxLength}个字符`,vm);
      return false
    }
    return true
  }
}
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    title:{
      value:"标题",
      type:String
    },
    value:{
      value:'',
      type:String
    },
    name:{
      value:"",
      type:String
    },
    placeholderText:{
      value:"请输入",
      type:String
    },
    textColor:{
      value:"#000",
      type:String
    },
    password:{
      value:false,
      type:Boolean
    },
    rule:{
      value:['noNone'],
      type:Array
    },
   
  },

  /**
   * 组件的初始数据
   */
  data: {
    show:true,
    text:''
  },
  
  /**
   * 组件的方法列表
   */
  methods: {
    onchange(e){
      let result;
      this.data.rule.forEach((rule)=>{
        const param = rule.split(':')[1];
        const ruleName = rule.split(':')[0];
        if(ruleFuc[ruleName]){
          let curResult = ruleFuc[ruleName](e,this.data.title,param,this);
          result = result === false ? result : curResult;

          if(result) this.setData({ show:true,text:''})
        }
      })
      this.triggerEvent('change',{ value:e.detail.value, name:this.data.name , result})
    },
  }
})
